<template>
  <el-dialog :title="`${form.patTypeId ? '修改' : '添加'}巡更类型`" :visible.sync="show" :close-on-click-modal="false" :show-close="!btnLoading" width="500px" @closed="$refs.form.resetFields()">
    <el-form ref="form" :model="form" :rules="rules" size="small" label-width="100px">
      <el-form-item prop="patTypeId" class="hide" />
      <el-form-item prop="parentId" label="上级">
        <treeselect v-model="form.parentId" :options="treeOptions" :normalizer="normalizer" show-count placeholder="选择上级" :disabled="isEdit" />
      </el-form-item>
      <el-form-item label="巡更类型名称" prop="patTypeName"><el-input v-model.trim="form.patTypeName" placeholder="请输入巡更类型名称" :maxlength="255" /></el-form-item>
      <el-form-item label="检查要求" prop="checkRequire"><el-input v-model="form.checkRequire" type="textarea" placeholder="请输入检查要求" :maxlength="2000" /></el-form-item>
      <el-form-item label="备注" prop="remark"><el-input v-model="form.remark" type="textarea" placeholder="请输入内容" :maxlength="255" /></el-form-item>
      <el-form-item label="排序" prop="seq"><el-input-number v-model="form.seq" :min="0" :max="1e8" :precision="0" controls-position="right" /></el-form-item>
      <el-form-item label="是否启用" prop="isEnable"><el-switch v-model="form.isEnable" :active-value="1" :inactive-value="0" /></el-form-item>
    </el-form>
    <div slot="footer">
      <el-button type="primary" :loading="btnLoading" @click="btnSubmit">确 定</el-button>
      <el-button :disabled="btnLoading" @click="show = false">取 消</el-button>
    </div>
  </el-dialog>
</template>
<script>
// 添加或修改巡更类型
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import { editFormMixin } from '@/utils/mixin'
import { addPatType, setPatType } from '@/api/cq/patrol'
export default {
  components: { Treeselect },
  mixins: [editFormMixin],
  data() {
    return {
      primaryKey: 'patTypeId',
      addApi: addPatType,
      updateApi: setPatType,
      form: {
        parentId: '',
        patTypeId: '',
        patTypeName: '',
        checkRequire: '',
        remark: '',
        seq: 0,
        isEnable: 1
      },
      rules: {
        patTypeName: [{ required: true, message: '请输入巡更类型名称' }]
      },
      treeOptions: []
    }
  },
  computed: {
    isEdit() {
      return !!this.form.patTypeId
    }
  },
  methods: {
    opened(item, { treeData }) {
      this.treeOptions = [{ patTypeId: '', patTypeName: '主类目', children: treeData }]
    },
    normalizer(node) {
      if (!node.children?.length) delete node.children
      return { id: node.patTypeId, label: node.patTypeName, children: node.children }
    }
  }
}
</script>
